from sqlalchemy import Column, String, Numeric, ForeignKey, DateTime, func, Text, Integer
from sqlalchemy.orm import relationship,backref

from database import db
from .fund_channel import FundChannel
from .gateway import Gateway


class GatewayOrder(db.Model):
    STATUS_WAITING_FOR_SEND = 'waiting_for_send'
    STATUS_SENT = "sent"
    STATUS_SENDING_FAILURE = "sending_failure"
    STATUS_SUCCESS = 'success'
    STATUS_FAILED = 'failed'
    id = Column(Integer, autoincrement=True, primary_key=True)
    next_submit_time = Column(DateTime, doc='下次提交时间', default=func.now())
    currency = Column(String(20), nullable=False, doc='币种')
    amount = Column(Numeric(precision=15, scale=4), nullable=False, doc='交易金额')
    status = Column(String(30), default=STATUS_WAITING_FOR_SEND, doc='状态')
    gateway_id = Column(ForeignKey(Gateway.id), doc='网关代码')
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now())
    fee = Column(Numeric(precision=15, scale=4), default=0.00)
    extension = Column(Text)
    gateway = relationship(Gateway)

class GatewayOrderResult(db.Model):
    STATUS_WAITING_FOR_SEND = 'waiting_for_send'
    STATUS_SENT = "sent"
    STATUS_UNKNOWN = 'unknown'
    STATUS_SUCCESS = 'success'
    STATUS_FAILED = 'failed'
    STATUS_WAIT_CREDENTIAL = 'wait_credential'
    id = Column(Integer, primary_key=True, autoincrement=True)
    amount = Column(Numeric(precision=15, scale=4), default=0.00)
    real_amount = Column(Numeric(precision=15, scale=4), default=0.00, doc='网关扣款金额')
    settlement_date = Column(DateTime, doc='网关结算日期')
    result_code = Column(String(200), nullable=False, doc='网关返回代码')
    result_message = Column(Text, doc='网关返回信息')
    extension = Column(Text, doc='扩展信息')
    credential = Column(Text)
    order_no = Column(String(120), doc='网关订单号', nullable=False)
    fee = Column(Numeric(precision=15, scale=4), default=0.00, doc='网关实际手续费')
    gateway_order_id = Column(ForeignKey(GatewayOrder.id))
    fund_channel_id = Column(ForeignKey(FundChannel.id))
    created_at = Column(DateTime, default=func.now())
    status = Column(String(20), default='created')
    fund_channel = relationship(FundChannel)
    gateway_order = relationship(GatewayOrder)


class TradeNotify(db.Model):
    id = Column(String(40), primary_key=True)
    info = Column(Text, nullable=False)
    method = Column(String(20), default='GET', nullable=False, doc='通知方式，默认使用get方式')
    status = Column(String(20), default='none', doc='通知状态， 默认为未通知')
    last_time = Column(DateTime, doc='最后通知时间')
    next_time = Column(DateTime, doc='预计下次通知时间')
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now())


class TradeNotifyHistory(db.Model):
    id = Column(Integer, primary_key=True, autoincrement=True)
    return_message = Column(Text, nullable=True, doc='通知对方返回信息')
    status = Column(String(20), nullable=False)
    taking_time = Column(Integer, nullable=False, doc='通知耗时，以秒计')
    created_at = Column(DateTime, default=func.now())
    trade_notify_id = Column(ForeignKey(TradeNotify.id))

class TradeOrder(db.Model):
    STATUS_SUCCESS = 'success'
    STATUS_PENDING = 'pending'
    STATUS_FAILED = 'failed'
    STATUS_WAIT_CREDENTIAL = 'wait_credential'

    id = Column(String(80), primary_key=True)
    description = Column(String(400), nullable=True)
    amount = Column(Numeric(precision=15, scale=4), nullable=False, doc='交易金额')
    type = Column(String(40), nullable=False, doc='交易类型， fundin -入款 ， fundout -出款')
    currency = Column(String(20), nullable=False, doc='币种代码')
    gateway_id = Column(ForeignKey(Gateway.id), doc='网关代码', nullable=False)
    status = Column(String(20), doc='状态', default='pending')
    fund_channel_id = Column(ForeignKey(FundChannel.id), doc='资金渠道', nullable=False)
    payment_time = Column(DateTime, default=func.now())
    remark = Column(Text)
    fee = Column(Numeric(precision=15, scale=4), default=0.00, doc='手续费')
    trade_code = Column(String(230), nullable=False, doc='第三方交易追踪号')
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now())
    biz_time = Column(DateTime, default=func.now())
    # notify_info = Column(Text, doc='通知信息')
    # notify_status = Column(String(20), doc='通知状态')
    # notify_time = Column(DateTime, doc='通知时间')
    # next_notify_time = Column(DateTime, default=func.now(), doc='预计下次通知时间')
    notify_id = Column(ForeignKey(TradeNotify.id), doc='通知Id')

    gateway_order_id = Column(ForeignKey(GatewayOrder.id))
    gateway_order = relationship(GatewayOrder, backref=backref('trade_orders'))
